/**
 * Created by Mark on 2018/4/15.
 * 商品详情的页面逻辑
 */

require("./product-detail.css");
require("page/common/nav/nav.js");
require("page/common/header/header.js");
var _product = require("service/product-service.js");
var _cart = require("service/cart-service.js");
var _mm = require("util/mm.js");
var templateDetail = require("./product-detail.string");

var page = {
    data: {
        productId: _mm.getUrlParam("productId") || "",
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        if (!this.data.productId) {//如果从URL上获取的productId为空，则返回首页
            _mm.goHome();
        } else {
            this.loadDetail();
        }
        //this.initJqzoom();
    },
    bindEvent: function () {
        this.clickImageEvent();
        this.clickCountBtnEvent();
        this.addCartEvent();
    },
    //1.页面初始化函数
    loadDetail: function () {//加载商品详情
        var html = "";
        var _this = this;
        $pageWrap = $(".page-wrap");
        _product.getProductDetail(this.data.productId,
            function (res) {
                _this.filter(res.data);//解析缩列图路径
                _this.data.detailInfo = res.data;//缓存请求回来的detail数据
                html = _mm.renderHtml(templateDetail, res.data);//渲染HTML
                $pageWrap.html(html);
            }, function (err) {
                $pageWrap.html("<p class='err-tip'>抱歉！找不到您要的商品</p>");
            });

    },

    //2.事件绑定函数
    clickImageEvent: function () {//2.1图片预览事件
        $(document).on("mouseenter", ".p-img-item", function () {
            var imageUrl = $(this).find(".p-img").attr("src");
            $(".main-img").attr("src", imageUrl);
        });

    },
    clickCountBtnEvent: function () {//2.2点击数量加减的事件
        var _this = this;
        $(document).on("click", ".p-count-btn", function () {
            var type = $(this).hasClass("plus") ? "plus" : "minus";
            var $pCount = $(".p-count");
            var currentCount = parseInt($pCount.val());
            var minCount = 1;
            var maxCount = _this.data.detailInfo.productStock || 1;
            if (type === "plus") {
                if (currentCount === maxCount) {
                    _mm.errorTips("客观!被再点了，已经到上限");
                    return;
                }
                $pCount.val(currentCount < maxCount ? currentCount + 1 : maxCount);
            }
            else if (type === "minus") {
                if (currentCount === minCount) {
                    _mm.errorTips("这么残忍！一件都不愿意买吗？");
                    return;
                }
                $pCount.val(currentCount > minCount ? currentCount - 1 : minCount);
            }

        });
    },

    addCartEvent: function () { //2.2点击加入购物车的事件
        var _this = this;
        $(document).on("click",".cart-add",function(){
            var username = $.trim($(".username").text()),
                sellerName = $.trim($("#sellerName").text());
           if(username == sellerName){
               if(window.confirm("确定要将自己发货的商品加入购物车吗？")){
                   _cart.addToCart({
                       productId : _this.data.productId,
                       count : $(".p-count").val()
                   },function(){//成功时的回调函数
                       window.location.href = "./result.html?type=cart-add";
                   },function(err){//失败的回调函数
                       _mm.errorTips(err.msg);
                   });
               }
           }else{
               _cart.addToCart({
                   productId : _this.data.productId,
                   count : $(".p-count").val()
               },function(){//成功时的回调函数
                   window.location.href = "./result.html?type=cart-add";
               },function(err){//失败的回调函数
                   _mm.errorTips(err.msg);
               });
           }
        });
    },

    //3.辅助函数
    filter: function (data) {//3.1数据匹配
        var host = data.imageHost,
            subImages = data.subImgsAddr;
        var images = subImages.split(",");
        data.subImages = images.map(function (imageUri) {
            return{
                url : host+imageUri
            }
        });

    },
    initJqzoom : function(){//3.2放大镜
        $(".jqzoom").jqzoom({
            zoomType: 'standard',
            lens:true,
            preloadImages: false,
            alwaysOn:false,
            zoomWidth: 200,
            zoomHeight: 200,
            xOffset:10,
            yOffset:0,
            position:'right'
        });
    }
}
$(function(){
    page.init();
});